<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$params=array(
	'from_type'=>1,
);
$this->model('model')->checkuser(0,$params);
if($_RQ['op']=='main'){
	$store_id=intval($_RQ['storeid']);
	$this->store_id=$store_id;
	$workerid=intval($_RQ['workerid']);
	$daydata=$_RQ['daydata'];
	if(empty($workerid)){
		exi('请先选择医生！','error','goback');
	}
	$return['worker']=pdo_get('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$workerid));
	$price=pdo_getcolumn('jy_yiliao_lesson',array('id'=>$return['worker']['lesson_id']),'price');
	//下单页
	$item=array();
	//提交数据
	$return['post'] = array(
		'lessonid'=>$return['worker']['lesson_id'],
		'storeid'=>$return['worker']['storeid'],
		'workerid'=>$workerid,
		'daydata'=>$daydata,
		'price'=>$return['worker']['price'],
	);
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$user=pdo_get('jy_yiliao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$user['name']=$user['name']?:$_SESSION['member']['nickname'];
	$user['mobile']=$user['mobile']?:$member['mobile'];
	$return['user']=$user;
	$return['post']['name']=$user['name'];
	$body=pdo_get('jy_yiliao_body',array('isfa'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$body['name']=$body['name']?$user['name']:
	$body['year']=date('Y',$body['agetime']);
	$body['year']=$body['year']>0?date('Y')-$body['year']:'无';
	$body['sex']=$body['sex']==1?'男':'女';
	$return['post']['body']=$body;
	$return['xieyi']=$this->plugin_setting('xieyi');
	$return['lat_lng']=$this->session('lat_lng')?:array('lat'=>'');
	$return['nowtime']=time();
	set_html('title','确认订单');
	exi($return);
}
if($_RQ['op']=='serve_time'){
	$post=get_postdata();
	$workerid=intval($post['workerid']);
	if(!$workerid){
		exi('请选择大夫！','error');
	}
	if(!$post['daydata']){
		exi('请重新选择日期','error');
	}
	$worker=pdo_get('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$workerid));
	$pais=pdo_get('jy_yiliao_lesson_pai',array('onday'=>$post['daydata'],'uniacid'=>$_SESSION['uniacid'],'workerid'=>$workerid));
	$ontime=strtotime($post['daydata']);
	$weekid=date("w",$ontime);
	$demoontimes=array('start'=>'9:00','end'=>'17:00');
	$pais['time_hour']=json_decode($pais['time_hour'],true);
	if($pais['time_hour']['end']){
			$serve_time=$this->model('lesson')->getserve_time($pais['time_hour']);
			$return['ontimes']=$pais['time_hour'];
			$store=pdo_get('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$wv['storeid']));
	}
	if(!$serve_time){
		$return['ontimes']=$demoontimes;
		$serve_time=$this->model('lesson')->getserve_time($demoontimes,$ontime);
		$store=pdo_get('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker['storeid']));
	}
	if($serve_time){
		foreach($serve_time as $k=>&$item){
			if($item['timestamp']<time() && time()>$ontime){
				unset($serve_time[$k]);
				continue;
			}
			//待支付 已支付，已确认，已完成订单数
			$num=pdo_count('jy_yiliao_lesson_log',array('worker_id'=>$workerid,'uniacid'=>$_SESSION['uniacid'],'daydata'=>$post['daydata'],'ontimestart'=>$item['start']),'id');
			if($num>0){
				$item['isman']=1;
			}else{
				$item['isman']=0;
			}
		}
	}
	$serve_time=array_values($serve_time);
	$store['lat_lng']=json_decode($store['lat_lng'],true);
	$return['store']=$store;
	$return['serve_time']=$serve_time;
	$return['success']=1;
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['lessonid']){
		exi('请选择科室！','error');
	}
	if(!$post['name']){
		exi('请填写报名人！','error');
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'storeid'=>intval($post['storeid']),
		'lesson_id'=>intval($post['lessonid']),
		'worker_id'=>intval($post['workerid']),
		'bodyid'=>intval($post['body']['id']),
		'remark'=>trim($post['remark']),
		'daydata'=>$post['daydata'],
		'ontimestart'=>$post['ontimestart'],
		'ontime'=>$post['ontime'],
	);
	if($post['isfee']){
		$new_order['money']=0;
	}else{
		$new_order['money']=pdo_getcolumn('jy_yiliao_worker',array('id'=>$new_order['worker_id']),'price');
	}
	$logid=model('jy_yiliao/lesson')->add_lesson_log($new_order);
	if($new_order['money']>0){
		$new_order['logid']=$logid;
		$order_id=model('jy_yiliao/order')->create($new_order,'',$url);
		$params=array(
			'id'=>$order_id,
			'title'=>'报名上课',
			'fee'=>$new_order['money'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_yiliao/create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_yiliao/order/detail/id='.$order_id,
			'error_url'=>'jy_yiliao/order/detail/id='.$order_id,
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_yiliao_order',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
		);
		exi($res);
	}else{
		exi('报名成功！','','/yuyue_order/');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_yiliao/order/');
	}
	$order=pdo_get('jy_yiliao_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_yiliao/order/');
	}
	if($order['pay']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['pay'].'|'.$payresult['fee'],'error','jy_yiliao/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
	);
	$url=custom_url('jy_yiliao/order/detail/id='.$order['id'],true);
	model('jy_yiliao/order')->order_pay($order['id'],$params,$url);
	exi('支付成功!');
}

?>